#include <iostream>
#include <string>
#include <unistd.h>
#include <sys/shm.h>
#include <sys/wait.h>

char* getSharedMemory()
{
    auto key = ::ftok("myfile", 42);
    auto shm = ::shmget(key, 0x1000, 0666 | IPC_CREAT);
    return static_cast<char*>(::shmat(shm, nullptr, 0));
}

using namespace std;
int main(int argc, char** argv)
{
    if (fork() != 0)
    {
        ::sleep(1);
        cout << "in parent: \n";
        auto msg = getSharedMemory();
        msg[0] = 42;
        wait(nullptr);
    }
    else
    {
        auto msg = getSharedMemory();
        while (msg[0] != 42)
            ;
        cout << "child get msg\n";
    }
    return 0;
}